//
//  BorderContentViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/22.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BorderContentViewController.h"
#import "BorderAminListmodel.h"
#import "UITableViewFootview.h"
#import "OrdercontTableViewCell.h"
#import "OrderContenhesderview.h"

@interface BorderContentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@property(nonatomic,assign)NSInteger dayind;
@property(nonatomic,strong)UITableViewFootview *footview;

@end

@implementation BorderContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单详情";
    // Do any additional setup after loading the view.
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    // Do any additional setup after loading the view.
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refashaction:) name:@"refashA" object:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _model.pros.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 345;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    OrderContenhesderview *veiw = [[OrderContenhesderview alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 140)];
    
    veiw.orderNumberLB.text = [NSString stringWithFormat:@"%@",_model.order_number];
    veiw.careTimeLb.text = [NSString stringWithFormat:@"%@",_model.create_time];
    veiw.paytimeLb.text = [NSString stringWithFormat:@"%@",_model.pay_time];
    veiw.addressNameLB.text = [NSString stringWithFormat:@"%@",_model.user_name];
    veiw.addressPhoneLB.text = [NSString stringWithFormat:@"%@",_model.phone];
    veiw.addressconterLB.text = [NSString stringWithFormat:@"%@",_model.address];
    if ([_model.send_type_id integerValue] == 0) {
        veiw.wuliutypeImage.image = imgname(@"shopziti");
    }else if ([_model.send_type_id integerValue] == 1){
        veiw.wuliutypeImage.image = imgname(@"shopwuliu");
        
    }else if ([_model.send_type_id integerValue] == 2){
        veiw.wuliutypeImage.image = imgname(@"shopdidipeisong");
        
    }
    

    NSInteger ind = [_model.state integerValue];
    //    状态(0代付款1待发货2待收货3待评价4已评价
    switch (ind) {
        case 0:
        {
            veiw.stateLb.text =@"待付款";
        }
            break;
        case 1:
        {
            veiw.stateLb.text =@"待发货";
            
        }
            break;
        case 2:
        {
            veiw.stateLb.text =@"待收货";
            
        }
            break;
        case 3:
        {
            veiw.stateLb.text =@"待评价";
            
        }
            break;
        case 4:
        {
            veiw.stateLb.text =@"已评价";
            
        }
            break;
        case -1:
        {
            veiw.stateLb.text =@"已取消";
            
        }
            break;
            
            
        default:
            break;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    if ([_model.state integerValue] < 3) {
        veiw.height = 108;
        veiw.fukuantop.constant = 0;
        veiw.fukuanHeight.constant = 0.1;
        veiw.paytimeLb.hidden = YES;
    }
    
    
    
    
    return veiw;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSInteger ind = [_model.state integerValue];
    //    状态(0代付款1待发货2待收货3待评价4已评价
    if (ind == 1) {
        return 67;
    }else{
        return 30;
    }

    return 0.1;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSInteger ind = [_model.state integerValue];
    //    状态(0代付款1待发货2待收货3待评价4已评价
  
    UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 30)];
    footview.backgroundColor  = [UIColor whiteColor];
    UILabel *priceLb = [[UILabel alloc]init];
    priceLb.textColor = zhutiColor;
    priceLb.font = FontSize(18);
    priceLb.text = [NSString stringWithFormat:@"¥ %@",_model.count_money];
    [footview addSubview:priceLb];
    [priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(3);
        make.height.offset(18);
        make.right.offset(-16);
    }];
    UILabel *heji = [[UILabel alloc]init];
    heji.textColor = [UIColor colorWithHexString:@"#4A4A4A"];
    heji.font = FontSize(12);
    heji.text = @"合计:";
    [footview addSubview:heji];
    [heji mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.height.offset(12);
        make.width.offset(30);
        make.right.mas_equalTo(priceLb.mas_left).offset(-2);
    }];
    
    UILabel *gongji = [[UILabel alloc]init];
    gongji.textColor =  [UIColor colorWithHexString:@"#4A4A4A"];
    gongji.font = FontSize(18);
    gongji.text = [NSString stringWithFormat:@"共计%.0f件产品",_model.count];
    [footview addSubview:gongji];
    [gongji mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(3);
        make.height.offset(18);
        make.right.mas_equalTo(heji.mas_left).offset(-14);
    }];
    if (ind == 1) {
        footview.height = 67;
        UIButton *fahuobut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [fahuobut setImage:imgname(@"fahuoshop") forState:(UIControlStateNormal)];
        [footview addSubview:fahuobut];
        [fahuobut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-16);
            make.width.offset(69);
            make.height.offset(26);
            make.bottom.offset(-5);
        }];
        [fahuobut addTarget:self action:@selector(fahuozation:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    
    
    
    return footview;
}
-(void)fahuozation:(UIButton *)but{
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"确定现在发货？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"取消");
    }]];
    kWeakSelf;
    [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"确定");
        NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
        param[@"id"] = NOTNIL(weakSelf.model.userID);
        
        [NetWorkTool POST:BshopdoSendProt param:param success:^(id dic) {
            ShowToastWithText(@"发货成功");
            if (self.mhyblcok) {
                self.mhyblcok(0);
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
    }]];
    
    [self presentViewController:alertCon animated:YES completion:nil];
    
    
    
    
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    状态(0代付款1待发货2待收货3待评价4已评价
    if ([_model.state integerValue] < 3) {
        return 108;
    }else{
    return 140;
        
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrdercontTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrdercontTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ProsModel *poM0del = [_model.pros safeObjectAtIndex:indexPath.row];
    [cell refashwithmodel:poM0del];
    
    
    
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStyleGrouped)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        //        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //            [_myTableView.mj_header beginRefreshing];
        //        }];
        
        [_myTableView registerNib:[UINib nibWithNibName:@"OrdercontTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrdercontTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
